﻿using HuiZe.education.Common;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace HuiZe.education.Resource.Controls
{
    public class CustomButton : Button
    {
        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(CustomButton), new PropertyMetadata(null));

        public ImageSource DeleteImageSource
        {
            get { return (ImageSource)GetValue(DeleteImageSourceProperty); }
            set { SetValue(DeleteImageSourceProperty, value); }
        }
        public static readonly DependencyProperty DeleteImageSourceProperty =
            DependencyProperty.Register("DeleteImageSource", typeof(ImageSource), typeof(CustomButton), new PropertyMetadata(null));

        public CornerRadius CornerRadius { get; set; }

        public int Index
        {
            get { return (int)GetValue(IndexProperty); }
            set { SetValue(IndexProperty, value); }
        }
        public static readonly DependencyProperty IndexProperty =
            DependencyProperty.Register("Index", typeof(int), typeof(CustomButton), new PropertyMetadata(1));
        public double DeleteHeight { get; set; }
        public double DeleteWidth { get; set; }
        public string SourceUrl { get; set; }
        public string MeadiaType { get; set; }
        public double ImageHeight { get; set; }
        public double ImageWidth { get; set; }
        public double BorderHeight { get; set; }
        public string textContent { get; set; }
        public string BkResId { get; set; }
        public bool IsDeleted { get; set; } = false;
        public int BkzyType { get; set; }
        public Thickness ImageMargin { get; set; } = new Thickness(10, 0, 0, 0);
        public Thickness TextMargin { get; set; } = new Thickness(0, 5, 0, 0);
        public CornerRadius ImageCornerRadius { get; set; }
        public CustomButton()
        {
            Loaded += CustomButton_Loaded;
            Unloaded += CustomButton_Unloaded;
        }

        private void CustomButton_Unloaded(object sender, RoutedEventArgs e)
        {
            CustomButton btn = CommonMethods.FindVisualChildItem<CustomButton>(this, "deleteButton");
            if (btn != null)
                btn.Click -= Btn_Click;

        }

        private void CustomButton_Loaded(object sender, RoutedEventArgs e)
        {
            CustomButton btn = CommonMethods.FindVisualChildItem<CustomButton>(this, "deleteButton");
            if (btn != null)
                btn.Click += Btn_Click;
        }



        public  void Btn_Click(object sender, RoutedEventArgs e)
        {
            var parent = CommonMethods.FindVisualParent<UniformGrid>(this);
            if (parent != null)
            {
                (parent as UniformGrid).Children.Remove(this);
                IsDeleted = true;
            }
            else 
            {
                IsDeleted = false;
            }

            
        }


    }
}
